﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using UFC.DBUtility;
using System.Collections;
using System.Configuration;
using System.Threading.Tasks;
using System.IO;

namespace SendIndustrialNetwork
{
    public class SaveRbMESData
    {
        public static string rbFactoryID = ConfigurationManager.AppSettings["RbFactoryID"].ToString();//热表分厂ID
        public static string toAppId = ConfigurationManager.AppSettings["ToAppID_RB"].ToString();//推送数据中心目的ID（热表）

        #region 执行入口

        /// <summary>
        /// 推送转工子卡
        /// </summary>
        public void ExcuteShipContainer()
        {
            if (SaveShipDataToChangeRecord())//推送转工批次
            {
                LogHelp.WriteLog(string.Format("(热表)转工子卡结束写入推送表"));
            }
        }
        /// <summary>
        /// 推送采购订单批次
        /// </summary>
        public void ExcuteOrderContainer()
        {
            SaveOrderContainerToChangeRecord();//推送采购订单批次
        }
        /// <summary>
        /// 推送其他数据
        /// </summary>
        public void ExcuteSupplementData()
        {
            SendDeleContainer();//补充已经删除的批次，但未推送的
            SendCommonTableData();//推送其他部分表的数据
        }

        #endregion

        #region 转工子卡的推送

        /// <summary>
        /// 获取转工到热表的转工信息列表
        /// </summary>
        /// <returns></returns>
        private DataTable GetContainerShipRecordData()
        {
            string strSql = string.Format(@"
                            SELECT Row_Number() OVER( ORDER BY ai.createdate) numer, cci.containerid,csc.containersubcontainerid,csr.containershiprecordid,fac.containerid facontainerid, 
                            csrs.containershiprecordstepid,ws.workflowstepid,sb.specbaseid,sc.specid,fac.productid,fac.productbaseid,fac.workflowid,fac.workflowbaseid
                            FROM containercurrentinfo cci 
                            INNER JOIN containersubcontainer csc ON csc.subcontainerid = cci.containerid
                            LEFT JOIN apsplaninfo ai ON ai.containerid = cci.containerid
                            LEFT JOIN containercurrentinfo fac ON fac.containerid = csc.containerid
                            LEFT JOIN containershiprecord csr ON csr.containerid = fac.containerid
                            LEFT JOIN containershiprecordstep csrs ON csrs.containershiprecordid = csr.containershiprecordid
                            LEFT JOIN workflowstep ws ON ws.workflowstepid = csrs.workflowstepid
                            LEFT JOIN specbase sb ON sb.specbaseid = ws.specbaseid
                            LEFT JOIN spec sc ON sc.specid = NVL(sb.revofrcdid,ws.specid)
                            WHERE cci.isinstore = 0 AND cci.containerstatus = 1 AND cci.originalfactoryid = '{0}' AND ai.isuse = 0 AND csr.isuse = 0
                            AND csr.toacceptdate IS NOT NULL AND csr.tofactoryid = '{0}' AND ws.workflowstepname = csc.workflowstepname
                            AND EXISTS (SELECT 1 FROM apsplandetail2 ap WHERE ap.containerid = cci.containerid AND ap.status = '1') --存在下发派工的
                            AND NOT EXISTS (SELECT 1 FROM containerstepworkreport2 csw WHERE csw.containerid = cci.containerid AND csw.isuse = 0) --不存在工步报工的
                            AND NOT EXISTS (SELECT 1 FROM sync_tablechangerecord_rb st WHERE st.tablename = UPPER('containercurrentinfo') AND st.uniquecolvalue = cci.containerid)
                            AND NOT EXISTS (SELECT 1 FROM sync_tablechangerecord st1 WHERE st1.tablename = UPPER('containercurrentinfo') AND st1.uniquecolvalue = cci.containerid)", rbFactoryID);
            DataTable shipDt = OracleHelper.QueryTable(strSql, null);
            return shipDt;
        }

        /// <summary>
        /// 保存转工记录到推送表中
        /// </summary>
        private bool SaveShipDataToChangeRecord()
        {
            /*-------------------------------------------------------------------------------------------------------
             * 简要说明： 以转工接收为触发点，展开对相关表数据插入到变更数据表中，最终推送给数据中心，数据中心推送给热表工控网
             -------------------------------------------------------------------------------------------------------*/
            DataTable shipDt = GetContainerShipRecordData();//获取转工到热表的转工记录表
            if (shipDt.Rows.Count == 0)
            {
                return true;
            }
            DataTable subShipDt = shipDt.AsDataView().ToTable(true, "containerid");//筛选出热表批次
            ParallelOptions parallelOption = new ParallelOptions();
            parallelOption.MaxDegreeOfParallelism = Environment.ProcessorCount / 2;
            LogHelp.WriteLog(string.Format("(热表)转工子卡开始写入推送表，待写入批次数量：{0}，并行数：{1}", shipDt.Rows.Count, parallelOption.MaxDegreeOfParallelism));
            Parallel.For(0, subShipDt.Rows.Count, parallelOption, i =>
            {
                DataRow row = subShipDt.Rows[i];
                /*-------------------------------------------------------------------------
                     * ----------查询出对应的热表子卡信息，如果没有子卡，直接不用推送---------------
                     -------------------------------------------------------------------------*/
                string sql = string.Format(@"SELECT cci.containerid,cci.containername,w.workflowid,wb.workflowbaseid,
                                p.productid,pb.productbaseid,ws.workflowstepid,sb.specbaseid,sc.specid
                                FROM containercurrentinfo cci
                                LEFT JOIN container c ON c.containerid = cci.containerid
                                LEFT JOIN workflow w ON w.workflowid = DECODE(cci.isinstore,-1,c.mfgworkflowid,cci.workflowid)
                                LEFT JOIN workflowbase wb ON wb.workflowbaseid = w.workflowbaseid
                                LEFT JOIN product p ON p.productid = cci.productid
                                LEFT JOIN productbase pb ON pb.productbaseid = p.productbaseid
                                LEFT JOIN workflowstep ws ON ws.workflowid = w.workflowid
                                LEFT JOIN specbase sb ON sb.specbaseid = ws.specbaseid
                                LEFT JOIN spec sc ON sc.specid = NVL(sb.revofrcdid,ws.specid)
                                WHERE cci.containerid = '{0}' AND cci.isinstore != 1 ", row["containerid"].ToString());
                DataTable dtSubContainer = OracleHelper.QueryTable(sql, null);
                if (dtSubContainer.Rows.Count == 0)
                {
                    return;
                }
                /*-------------------------------------------------------------------------
                 * ----------查询子卡是否有报工记录了，有了就不推送---------------------
                 -------------------------------------------------------------------------*/
                DataRow subContainerRow = dtSubContainer.Rows[0];
                sql = string.Format("SELECT 1 FROM containerstepworkreport2 csw WHERE csw.containerid = '{0}' AND csw.isuse = 0", subContainerRow["containerid"].ToString());
                if (OracleHelper.QueryTable(sql, null).Rows.Count > 0) //如果有了报工记录，不用推送
                {
                    return;
                }

                /*----------------------------------------------------------------------------------------
                 * ----------查询子卡的工步，工步的排产信息。如果没有，直接不用推送----------
                 ----------------------------------------------------------------------------------------*/
                sql = string.Format(@"SELECT s.stepinfoid,sib.stepinfobaseid
                                FROM workflowstep ws 
                                LEFT JOIN stepinfo s ON s.workflowstepid = ws.workflowstepid
                                INNER JOIN stepinfobase sib ON sib.revofrcdid = s.Stepinfoid --取最新版
                                WHERE ws.workflowid = '{0}'", subContainerRow["workflowid"].ToString());
                DataTable dtStepInfo = OracleHelper.QueryTable(sql, null);
                if (dtStepInfo.Rows.Count == 0)
                {
                    return;
                }
                string stepInfoIDs = string.Empty; //工步ID集
                foreach (DataRow item in dtStepInfo.AsDataView().ToTable(true, "stepinfoid").Rows)
                {
                    stepInfoIDs += string.Format("'{0}',", item["stepinfoid"].ToString());
                }
                stepInfoIDs = stepInfoIDs.TrimEnd(',');
                /*-------------------------------------------------------------------------
                 * -------------查询并推送工步的派工、派工的设备、设备下的人员---------------
                 --------------------------------------------------------------------------*/
                if (!SendStepInfoDispatch(subContainerRow["containerid"].ToString(), stepInfoIDs))
                {
                    return;
                }
                /*---------------------------------------------------------------------
                 * ----------------------推送子卡相关表数据----------------------
                 -------------------------------------------------------------------*/
                SendRbContainer(dtSubContainer);
                /*---------------------------------------------------------------------
                 * ----------------------推送工步基础信息数据----------------------
                 -------------------------------------------------------------------*/
                SendStepInfo(dtStepInfo, stepInfoIDs, subContainerRow);
                /*----------------------------------------------------------------
                 * -----------------查询并推送工步的工装工具、辅材----------------------
                 -----------------------------------------------------------------*/
                SendStepInfoTool(stepInfoIDs);
                /*----------------------------------------------------------------
                 * -----------------查询并推送工序的工装工具、辅材----------------------
                 -----------------------------------------------------------------*/
                SendWorkflowStepTool(subContainerRow["workflowstepid"].ToString());
                /*----------------------------------------------------------------
                * -----------------查询并推送批次的特殊过程报告----------------------
                -----------------------------------------------------------------*/
                SendSpecialCourseSpecReport(subContainerRow["containerid"].ToString(), subContainerRow["workflowid"].ToString(), 1);
                /*-------------------------------------------------------------------------
                * -----------------推送主卡相关表数据---------------------------------
                -------------------------------------------------------------------------*/
                DataTable dtMainCon = shipDt.Select(string.Format("containerid = '{0}'", row["containerid"].ToString())).CopyToDataTable();
                SendMainContainer(dtMainCon);
            });
            return true;
        }

        #endregion

        #region 采购订单批次推送

        /// <summary>
        /// 获取热表采购订单批次信息列表
        /// </summary>
        /// <returns></returns>
        private DataTable GetContainerOrderData()
        {
            var strSql = string.Format(@"--采购订单卡
                            SELECT distinct cci.containerid FROM containercurrentinfo cci 
                            INNER JOIN userattribute u ON u.parentid = cci.containerid AND u.userattributename = '委托批次'
                            WHERE cci.isinstore = 0 AND cci.containerstatus = 1 AND cci.originalfactoryid = '{0}'
                            AND EXISTS (SELECT 1 FROM rbordercontainerreceive r WHERE r.containerid = cci.containerid AND r.receivedate IS NOT NULL AND r.completedate IS NULL)
                            AND EXISTS (SELECT 1 FROM apsplandetail2 ap WHERE ap.containerid = cci.containerid AND ap.status = '1') --存在下发派工的
                            AND NOT EXISTS (SELECT 1 FROM containerstepworkreport2 csw WHERE csw.containerid = cci.containerid AND csw.isuse = 0) --不存在工步报工的
                           AND NOT EXISTS (SELECT 1 FROM sync_tablechangerecord_rb st WHERE st.tablename = UPPER('containercurrentinfo') AND st.uniquecolvalue = cci.containerid)
                            AND NOT EXISTS (SELECT 1 FROM sync_tablechangerecord st1 WHERE st1.tablename = UPPER('containercurrentinfo') AND st1.uniquecolvalue = cci.containerid)
                            UNION ALL
                            --返修卡
                            SELECT distinct cci.containerid FROM containercurrentinfo cci 
                            INNER JOIN userattribute u ON u.parentid = cci.containerid AND u.userattributename = '返修主批次ID'
                            WHERE cci.isinstore = 0 AND cci.containerstatus = 1 AND cci.originalfactoryid = '{0}'
                            AND EXISTS (SELECT 1 FROM apsplandetail2 ap WHERE ap.containerid = cci.containerid AND ap.status = '1') --存在下发派工的
                            AND NOT EXISTS (SELECT 1 FROM containerstepworkreport2 csw WHERE csw.containerid = cci.containerid AND csw.isuse = 0) --不存在工步报工的
                            AND NOT EXISTS (SELECT 1 FROM sync_tablechangerecord_rb st WHERE st.tablename = UPPER('containercurrentinfo') AND st.uniquecolvalue = cci.containerid)
                            AND NOT EXISTS (SELECT 1 FROM sync_tablechangerecord st1 WHERE st1.tablename = UPPER('containercurrentinfo') AND st1.uniquecolvalue = cci.containerid)", rbFactoryID);
            DataTable dtContainer = OracleHelper.QueryTable(strSql, null);
            return dtContainer;
        }

        /// <summary>
        /// 保存采购订单批次到推送表中
        /// </summary>
        private void SaveOrderContainerToChangeRecord()
        {
            /*-------------------------------------------------------------------------------------------------------
            * 简要说明： 以采购订单批次接收为触发点，展开对相关表数据插入到变更数据表中，最终推送给数据中心，数据中心推送给热表工控网
            -------------------------------------------------------------------------------------------------------*/
            DataTable dtContainer = GetContainerOrderData();//获取热表采购订单批次信息列表
            foreach (DataRow row in dtContainer.Rows)
            {
                /*-------------------------------------------------------------------------
                 * ----------查询是否有报工记录了，有了就不推送---------------------
                 -------------------------------------------------------------------------*/
                string sql = string.Format("SELECT 1 FROM containerstepworkreport2 csw WHERE csw.containerid = '{0}' AND csw.isuse = 0", row["containerid"].ToString());
                if (OracleHelper.QueryTable(sql, null).Rows.Count > 0)
                {
                    continue;
                }
                /*-------------------------------------------------------------------------
                 * ----------查询出采购订单卡的工艺信息---------------
                 -------------------------------------------------------------------------*/
                sql = string.Format(@" SELECT cci.containerid, w.workflowid,wb.workflowbaseid,p.productid,pb.productbaseid,ws.workflowstepid,sb.specbaseid,sc.specid 
                                FROM containercurrentinfo cci 
                                LEFT JOIN workflow w ON w.workflowid = cci.workflowid
                                LEFT JOIN workflowbase wb ON wb.workflowbaseid = w.workflowbaseid
                                LEFT JOIN product p ON p.productid = cci.productid
                                LEFT JOIN productbase pb ON pb.productbaseid = p.productbaseid
                                LEFT JOIN workflowstep ws ON ws.workflowid = w.workflowid
                                LEFT JOIN specbase sb ON sb.specbaseid = ws.specbaseid
                                LEFT JOIN spec sc ON sc.specid = NVL(sb.revofrcdid,ws.specid)
                                WHERE cci.containerid = '{0}'", row["containerid"].ToString());
                DataTable dtRbContainer = OracleHelper.QueryTable(sql, null);

                DataRow drCon = dtRbContainer.Rows[0];
                /*----------------------------------------------------------------------------------------
                 * ----------查询批次的工步，工步的排产信息，如果没有，直接不用推送----------
                 ----------------------------------------------------------------------------------------*/
                sql = string.Format(@"SELECT s.stepinfoid,sib.stepinfobaseid,ai.apsplaninfoid,ap.apsplandetailid,sp.stepcheckpointid
                                FROM workflowstep ws 
                                LEFT JOIN stepinfo s ON s.workflowstepid = ws.workflowstepid
                                INNER JOIN stepinfobase sib ON sib.revofrcdid = s.Stepinfoid --取最新版
                                INNER JOIN apsplandetail2 ap ON ap.stepinfoid = s.stepinfoid AND ap.isuse = 0
                                LEFT JOIN apsplaninfo ai ON ai.apsplaninfoid = ap.apsplaninfoid
                                LEFT JOIN stepcheckpoint sp ON sp.stepinfoid = s.stepinfoid
                                WHERE ws.workflowid = '{0}' AND ai.containerid = '{1}' AND ai.isuse = 0 ", drCon["workflowid"].ToString(), row["containerid"].ToString());
                DataTable dtStepInfo = OracleHelper.QueryTable(sql, null);
                if (dtStepInfo.Rows.Count == 0)
                {
                    continue;
                }
                string stepInfoIDs = string.Empty; //工步ID集
                foreach (DataRow item in dtStepInfo.AsDataView().ToTable(true, "stepinfoid").Rows)
                {
                    stepInfoIDs += string.Format("'{0}',", item["stepinfoid"].ToString());
                }
                stepInfoIDs = stepInfoIDs.TrimEnd(',');
                /*-------------------------------------------------------------------------
                 * -------------查询并推送工步的派工、派工的设备、设备下的人员---------------
                 --------------------------------------------------------------------------*/
                if (!SendStepInfoDispatch(row["containerid"].ToString(), stepInfoIDs))
                {
                    continue;//没有派工数据则不推送
                }
                /*---------------------------------------------------------------------
                 * ----------------------推送批次相关表数据----------------------
                 -------------------------------------------------------------------*/
                SendRbContainer(dtRbContainer);
                /*---------------------------------------------------------------------
                 * ----------------------推送工步基础信息数据----------------------
                 -------------------------------------------------------------------*/
                SendStepInfo(dtStepInfo, stepInfoIDs, drCon);
                /*----------------------------------------------------------------
                 * -----------------查询并推送工步的工装工具、辅材----------------------
                 -----------------------------------------------------------------*/
                SendStepInfoTool(stepInfoIDs);
                /*----------------------------------------------------------------
                 * -----------------查询并推送工序的工装工具、辅材----------------------
                 -----------------------------------------------------------------*/
                SendWorkflowStepTool(drCon["workflowstepid"].ToString());
                /*----------------------------------------------------------------
                * -----------------查询并推送批次的特殊过程报告----------------------
                -----------------------------------------------------------------*/
                SendSpecialCourseSpecReport(row["containerid"].ToString(), drCon["workflowid"].ToString(), 2);
            }
        }

        #endregion

        #region 推送通用方法
        /// <summary>
        /// 推送热表批次数据
        /// </summary>
        /// <param name="dtRbContainer"></param>
        private void SendRbContainer(DataTable dtRbContainer)
        {
            DataView dvSubContainer = dtRbContainer.AsDataView();
            /*推送批次信息*/
            CommonMethods.InsertTableChangeRecord_RB(dvSubContainer.ToTable(true, "containerid"), "container", "containerid", CommonMethods.OpType.新增);
            CommonMethods.InsertTableChangeRecord_RB(dvSubContainer.ToTable(true, "containerid"), "containercurrentinfo", "containerid", CommonMethods.OpType.新增);
            /*推送批次工艺信息*/
            CommonMethods.InsertTableChangeRecord_RB(dvSubContainer.ToTable(true, "workflowid"), "workflow", "workflowid", CommonMethods.OpType.新增);
            CommonMethods.InsertTableChangeRecord_RB(dvSubContainer.ToTable(true, "workflowbaseid"), "workflowbase", "workflowbaseid", CommonMethods.OpType.新增);
            /*推送批次件号信息*/
            CommonMethods.InsertTableChangeRecord_RB(dvSubContainer.ToTable(true, "productid"), "product", "productid", CommonMethods.OpType.新增);
            CommonMethods.InsertTableChangeRecord_RB(dvSubContainer.ToTable(true, "productbaseid"), "productbase", "productbaseid", CommonMethods.OpType.新增);
            /*推送批次工序信息*/
            CommonMethods.InsertTableChangeRecord_RB(dvSubContainer.ToTable(true, "workflowstepid"), "workflowstep", "workflowstepid", CommonMethods.OpType.新增);
            CommonMethods.InsertTableChangeRecord_RB(dvSubContainer.ToTable(true, "specbaseid"), "specbase", "specbaseid", CommonMethods.OpType.新增);
            CommonMethods.InsertTableChangeRecord_RB(dvSubContainer.ToTable(true, "specid"), "spec", "specid", CommonMethods.OpType.新增);
            /*推送批次属性信息*/
            string sql = string.Format("SELECT distinct u.userattributeid FROM userattribute u WHERE u.parentid = '{0}'", dtRbContainer.Rows[0]["containerid"].ToString());
            DataTable dtUserAttr = OracleHelper.QueryTable(sql, null);
            CommonMethods.InsertTableChangeRecord_RB(dtUserAttr, "userattribute", "userattributeid", CommonMethods.OpType.新增);
            /*推送批次子序号信息*/
            sql = string.Format("SELECT c.containerid FROM container c WHERE c.parentcontainerid = '{0}' AND c.status = 1", dtRbContainer.Rows[0]["containerid"].ToString());
            DataTable dtChildCon = OracleHelper.QueryTable(sql, null);
            if (dtChildCon.Rows.Count > 0)
            {
                CommonMethods.InsertTableChangeRecord_RB(dtChildCon, "container", "containerid", CommonMethods.OpType.新增);
            }
            /*---------------------------------------------------------------------
             * ----------------------推送子卡工艺附件数据----------------------
             -------------------------------------------------------------------*/
            string specids = string.Empty;
            foreach (DataRow drws in dvSubContainer.ToTable(true, "specid").Rows)
            {
                specids += string.Format("'{0}',", drws["specid"].ToString());
            }
            specids = specids.TrimEnd(',');
            sql = string.Format("select distinct t.objectdocinfoid from objectdocinfo t where t.parentid = '{0}' OR t.parentid IN ({1})", dtRbContainer.Rows[0]["workflowid"].ToString(), specids);
            DataTable dtObjDoc = OracleHelper.QueryTable(sql, null);
            CommonMethods.InsertTableChangeRecord_RB(dtObjDoc, "objectdocinfo", "objectdocinfoid", CommonMethods.OpType.新增);
        }

        /// <summary>
        /// 推送工步数据
        /// </summary>
        /// <param name="dtStepInfo"></param>
        /// <param name="stepInfoIDs"></param>
        /// <param name="drContainer"></param>
        private void SendStepInfo(DataTable dtStepInfo, string stepInfoIDs, DataRow drContainer)
        {
            DataView dvStepInfo = dtStepInfo.AsDataView();
            /*推送批次工步信息*/
            CommonMethods.InsertTableChangeRecord_RB(dvStepInfo.ToTable(true, "stepinfoid"), "stepinfo", "stepinfoid", CommonMethods.OpType.新增);
            CommonMethods.InsertTableChangeRecord_RB(dvStepInfo.ToTable(true, "stepinfobaseid"), "stepinfobase", "stepinfobaseid", CommonMethods.OpType.新增);
            /*推送排产信息*/
            string sql = string.Format(@"SELECT ai.apsplaninfoid,ap.apsplandetailid FROM apsplaninfo ai
                           LEFT JOIN apsplandetail2 ap ON ap.apsplaninfoid = ai.apsplaninfoid
                           WHERE ai.containerid = '{0}' AND ai.isuse = 0 AND ap.status = 1", drContainer["containerid"].ToString());
            DataTable dtAps = OracleHelper.QueryTable(sql, null);
            DataView dvAps = dtAps.AsDataView();
            CommonMethods.InsertTableChangeRecord_RB(dvAps.ToTable(true, "apsplaninfoid"), "apsplaninfo", "apsplaninfoid", CommonMethods.OpType.新增);
            CommonMethods.InsertTableChangeRecord_RB(dvAps.ToTable(true, "apsplandetailid"), "apsplandetail2", "apsplandetailid", CommonMethods.OpType.新增);
            /*推送工步的检测项信息*/
            sql = string.Format(@"SELECT distinct sp.stepcheckpointid FROM stepcheckpoint sp 
                                WHERE sp.workflowstepid = '{0}' AND sp.stepinfoid IN ({1})", drContainer["workflowstepid"].ToString(), stepInfoIDs);
            DataTable dtStepCheckPoint = OracleHelper.QueryTable(sql, null);
            CommonMethods.InsertTableChangeRecord_RB(dtStepCheckPoint, "stepcheckpoint", "stepcheckpointid", CommonMethods.OpType.新增);
        }

        /// <summary>
        /// 推送工步的派工相关数据（如果没有数据返回False）
        /// </summary>
        /// <param name="containerID"></param>
        /// <param name="stepInfoIDs"></param>
        private bool SendStepInfoDispatch(string containerID, string stepInfoIDs)
        {
            string sql = string.Format(@"SELECT csd.containerstepdispatchid,wc.workcenterid,t.teamid,r.resourceid,es.employeeresourcelistid,e.employeeid 
                                    FROM containerstepdispatch2 csd
                                    LEFT JOIN Workcenter wc ON wc.workcenterid = csd.workcenterid
                                    LEFT JOIN team t ON t.teamid = csd.teamid 
                                    LEFT JOIN Resourcedef r ON r.resourceid = csd.resourceid
                                    LEFT JOIN employeeresourcelist es ON es.resourcelistid = r.resourceid
                                    LEFT JOIN employee e ON e.employeeid = es.employeeid
                                    WHERE csd.containerid = '{0}' AND csd.stepinfoid IN ({1}) AND csd.isuse = 0", containerID, stepInfoIDs);
            DataTable dtDis = OracleHelper.QueryTable(sql, null);
            if (dtDis.Rows.Count == 0) //如果工步没有派工信息，不用推送
            {
                return false;
            }
            DataView dvDis = dtDis.AsDataView();
            /*推送工步的派工表信息*/
            CommonMethods.InsertTableChangeRecord_RB(dvDis.ToTable(true, "containerstepdispatchid"), "containerstepdispatch2", "containerstepdispatchid", CommonMethods.OpType.新增);
            /*推送工步派工的工区信息*/
            CommonMethods.InsertTableChangeRecord_RB(dvDis.ToTable(true, "workcenterid"), "workcenter", "workcenterid", CommonMethods.OpType.新增);
            /*推送工步派工的班组信息*/
            CommonMethods.InsertTableChangeRecord_RB(dvDis.ToTable(true, "teamid"), "team", "teamid", CommonMethods.OpType.新增);
            /*推送派工下设备信息*/
            CommonMethods.InsertTableChangeRecord_RB(dvDis.ToTable(true, "resourceid"), "resourcedef", "resourceid", CommonMethods.OpType.新增);
            /*推送设备人员关系表信息*/
            CommonMethods.InsertTableChangeRecord_RB(dvDis.ToTable(true, "employeeresourcelistid"), "employeeresourcelist", "employeeresourcelistid", CommonMethods.OpType.新增);
            /*推送人员信息*/
            CommonMethods.InsertTableChangeRecord_RB(dvDis.ToTable(true, "employeeid"), "employee", "employeeid", CommonMethods.OpType.新增);
            /*推送工序的派工表信息*/
            sql = string.Format(@"SELECT csd.containerstepdispatchid FROM containerstepdispatch csd WHERE csd.containerid = '{0}' AND csd.isuse = 0", containerID);
            DataTable dtDis1 = OracleHelper.QueryTable(sql, null);
            CommonMethods.InsertTableChangeRecord_RB(dtDis1, "containerstepdispatch", "containerstepdispatchid", CommonMethods.OpType.新增);
            return true;
        }

        /// <summary>
        /// 推送工步的辅材、工装工具数据
        /// </summary>
        /// <param name="stepInfoIDs"></param>
        private void SendStepInfoTool(string stepInfoIDs)
        {
            string sql = string.Format(@"SELECT sit.stepinfotoolsid,mt.measuretoolid,st.specialtoolid,ct.cuttoolid,ax.auxmaterialid
                        FROM stepinfotools sit 
                        LEFT JOIN measuretool mt ON mt.measuretoolid = sit.tools
                        LEFT JOIN specialtool st ON st.specialtoolid = sit.tools
                        LEFT JOIN cuttool ct ON ct.cuttoolid = sit.tools
                        LEFT JOIN auxmaterial ax ON ax.auxmaterialid = sit.tools
                        WHERE sit.stepinfoid IN ({0})", stepInfoIDs);
            DataTable dtTool = OracleHelper.QueryTable(sql, null);
            DataView dvTool = dtTool.AsDataView();
            /*推送工步的工具辅材关系表信息*/
            CommonMethods.InsertTableChangeRecord_RB(dvTool.ToTable(true, "stepinfotoolsid"), "stepinfotools", "stepinfotoolsid", CommonMethods.OpType.新增);
            /*推送工步的量具信息*/
            CommonMethods.InsertTableChangeRecord_RB(dvTool.ToTable(true, "measuretoolid"), "measuretool", "measuretoolid", CommonMethods.OpType.新增);
            /*推送工步的夹模具信息*/
            CommonMethods.InsertTableChangeRecord_RB(dvTool.ToTable(true, "specialtoolid"), "specialtool", "specialtoolid", CommonMethods.OpType.新增);
            /*推送工步的刀具信息*/
            CommonMethods.InsertTableChangeRecord_RB(dvTool.ToTable(true, "cuttoolid"), "cuttool", "cuttoolid", CommonMethods.OpType.新增);
            /*推送工步的辅材信息*/
            CommonMethods.InsertTableChangeRecord_RB(dvTool.ToTable(true, "auxmaterialid"), "auxmaterial", "auxmaterialid", CommonMethods.OpType.新增);
        }

        /// <summary>
        /// 推送工序的工装工具和辅材
        /// </summary>
        /// <param name="workflowstepID"></param>
        private void SendWorkflowStepTool(string workflowstepID)
        {
            string sql = string.Format(@"SELECT wt.workflowsteptoolsid,mt.measuretoolid,st.specialtoolid,ct.cuttoolid,ax.auxmaterialid 
                        FROM workflowsteptools wt 
                        LEFT JOIN measuretool mt ON mt.measuretoolid = wt.tools
                        LEFT JOIN specialtool st ON st.specialtoolid = wt.tools
                        LEFT JOIN cuttool ct ON ct.cuttoolid = wt.tools
                        LEFT JOIN auxmaterial ax ON ax.auxmaterialid = wt.tools 
                        WHERE wt.workflowstepid = '{0}'", workflowstepID);
            DataTable dtTool = OracleHelper.QueryTable(sql, null);
            DataView dvTool = dtTool.AsDataView();
            /*推送工步的工具辅材关系表信息*/
            CommonMethods.InsertTableChangeRecord_RB(dvTool.ToTable(true, "workflowsteptoolsid"), "workflowsteptools", "workflowsteptoolsid", CommonMethods.OpType.新增);
            /*推送工步的量具信息*/
            CommonMethods.InsertTableChangeRecord_RB(dvTool.ToTable(true, "measuretoolid"), "measuretool", "measuretoolid", CommonMethods.OpType.新增);
            /*推送工步的夹模具信息*/
            CommonMethods.InsertTableChangeRecord_RB(dvTool.ToTable(true, "specialtoolid"), "specialtool", "specialtoolid", CommonMethods.OpType.新增);
            /*推送工步的刀具信息*/
            CommonMethods.InsertTableChangeRecord_RB(dvTool.ToTable(true, "cuttoolid"), "cuttool", "cuttoolid", CommonMethods.OpType.新增);
            /*推送工步的辅材信息*/
            CommonMethods.InsertTableChangeRecord_RB(dvTool.ToTable(true, "auxmaterialid"), "auxmaterial", "auxmaterialid", CommonMethods.OpType.新增);
        }

        /// <summary>
        /// 推送主卡的数据
        /// </summary>
        /// <param name="dtMainCon"></param>
        private void SendMainContainer(DataTable dtMainCon)
        {
            DataView dvMainCon = dtMainCon.AsDataView();
            DataTable dtMainID = dvMainCon.ToTable(true, "facontainerid");//可能是多个主卡
            dtMainID.Columns["facontainerid"].ColumnName = "containerid";
            /*推送批次信息*/
            CommonMethods.InsertTableChangeRecord_RB(dtMainID, "container", "containerid", CommonMethods.OpType.新增);
            CommonMethods.InsertTableChangeRecord_RB(dtMainID, "containercurrentinfo", "containerid", CommonMethods.OpType.新增);
            CommonMethods.InsertTableChangeRecord_RB(dvMainCon.ToTable(true, "containersubcontainerid"), "containersubcontainer", "containersubcontainerid", CommonMethods.OpType.新增);
            /*推送批次工艺信息*/
            CommonMethods.InsertTableChangeRecord_RB(dvMainCon.ToTable(true, "workflowid"), "workflow", "workflowid", CommonMethods.OpType.新增);
            CommonMethods.InsertTableChangeRecord_RB(dvMainCon.ToTable(true, "workflowbaseid"), "workflowbase", "workflowbaseid", CommonMethods.OpType.新增);
            /*推送批次件号信息*/
            CommonMethods.InsertTableChangeRecord_RB(dvMainCon.ToTable(true, "productid"), "product", "productid", CommonMethods.OpType.新增);
            CommonMethods.InsertTableChangeRecord_RB(dvMainCon.ToTable(true, "productbaseid"), "productbase", "productbaseid", CommonMethods.OpType.新增);
            /*推送批次工序信息*/
            CommonMethods.InsertTableChangeRecord_RB(dvMainCon.ToTable(true, "workflowstepid"), "workflowstep", "workflowstepid", CommonMethods.OpType.新增);
            CommonMethods.InsertTableChangeRecord_RB(dvMainCon.ToTable(true, "specbaseid"), "specbase", "specbaseid", CommonMethods.OpType.新增);
            CommonMethods.InsertTableChangeRecord_RB(dvMainCon.ToTable(true, "specid"), "spec", "specid", CommonMethods.OpType.新增);
            /*推送批次转工信息*/
            CommonMethods.InsertTableChangeRecord_RB(dvMainCon.ToTable(true, "containershiprecordid"), "containershiprecord", "containershiprecordid", CommonMethods.OpType.新增);
            CommonMethods.InsertTableChangeRecord_RB(dvMainCon.ToTable(true, "containershiprecordstepid"), "containershiprecordstep", "containershiprecordstepid", CommonMethods.OpType.新增);
            /*推送批次属性信息*/
            string containerIds = string.Empty;
            foreach (DataRow item in dtMainID.Rows)
            {
                containerIds = string.Format("'{0}',", item["containerid"].ToString());
            }
            containerIds = containerIds.TrimEnd(',');
            string sql = string.Format("SELECT distinct u.userattributeid FROM userattribute u WHERE u.parentid IN ({0})", containerIds);
            DataTable dtUserAttr = OracleHelper.QueryTable(sql, null);
            CommonMethods.InsertTableChangeRecord_RB(dtUserAttr, "userattribute", "userattributeid", CommonMethods.OpType.新增);
        }

        /// <summary>
        /// 推送特殊过程报告
        /// </summary>
        /// <param name="containerID"></param>
        /// <param name="workflowID"></param>
        /// <param name="opType">1：转工子卡，2：采购订单卡</param>
        private void SendSpecialCourseSpecReport(string containerID, string workflowID, int opType)
        {
            string insertContainerAttrSql = "INSERT INTO containerattribute(containerattributeid,containerid,attributename,attributevalue) VALUES(sys_guid(),'{0}','{1}','{2}')";
            string updateContainerAttrSql = "UPDATE containerattribute ca SET ca.attributevalue = '{0}' WHERE ca.containerid = '{1}' AND ca.attributename = '{2}'";
            string seleContainerAttrSql = "SELECT ca.* FROM containerattribute ca WHERE ca.containerid = '{0}' AND ca.attributename = '{1}'";
            string sql = string.Empty;
            if (opType == 1)
            {
                sql = string.Format(@"SELECT s1.specid FROM containercurrentinfo cti 
                        left join workflow w on w.workflowid = cti.workflowid
                        left join workflowbase wb on wb.workflowbaseid = w.workflowbaseid
                        LEFT JOIN spec s1 ON s1.commonsubworkflow = replace(wb.workflowname,'WF-','')
                        WHERE cti.containerid = '{0}' AND s1.specid IS NOT NULL", containerID);
                DataTable dtThreeSpec = OracleHelper.QueryTable(sql, null);
                if (dtThreeSpec.Rows.Count > 0)
                {
                    sql = string.Format(@"
                            SELECT ww.workflowid,ww.workflowbaseid,wws.workflowstepid,sb.specbaseid,s.specid,scs.specialcoursespecmappid,ssr.specialcoursespecreportid FROM containersubcontainer cc 
                            LEFT JOIN stepsubstep sbs ON sbs.workflowstepid = cc.workflowstepid 
                            LEFT JOIN workflow ww ON ww.workflowid = sbs.sonworkflowid
                            LEFT JOIN workflowstep wws on wws.workflowstepid = sbs.sonworkflowstepid
                            LEFT JOIN specbase sb on sb.specbaseid=wws.specbaseid
                            LEFT JOIN spec s on s.specid=nvl(sb.revofrcdid,wws.specid)
                            LEFT JOIN SpecialCourseSpecMapp scs on scs.workflowstepid = wws.workflowstepid
                            LEFT JOIN SpecialCourseSpecReport ssr ON ssr.specialcoursespecreportid = scs.specialcoursespecreportid
                            WHERE cc.subcontainerid= '{0}'", containerID);
                    DataTable dtSecSpec = OracleHelper.QueryTable(sql, null);//去第二层的工艺信息及特殊过程报告
                    DataView dvSecSpec = dtSecSpec.AsDataView();
                    CommonMethods.InsertTableChangeRecord_RB(dvSecSpec.ToTable(true, "workflowid"), "workflow", "workflowid", CommonMethods.OpType.新增);
                    CommonMethods.InsertTableChangeRecord_RB(dvSecSpec.ToTable(true, "workflowbaseid"), "workflowbase", "workflowbaseid", CommonMethods.OpType.新增);
                    CommonMethods.InsertTableChangeRecord_RB(dvSecSpec.ToTable(true, "workflowstepid"), "workflowstep", "workflowstepid", CommonMethods.OpType.新增);
                    CommonMethods.InsertTableChangeRecord_RB(dvSecSpec.ToTable(true, "specbaseid"), "specbase", "specbaseid", CommonMethods.OpType.新增);
                    CommonMethods.InsertTableChangeRecord_RB(dvSecSpec.ToTable(true, "specid"), "spec", "specid", CommonMethods.OpType.新增);
                    //推送特殊过程时，如果有则更新推送，没有则新增推送
                    CommonMethods.InsertTableChangeRecord_RB(dvSecSpec.ToTable(true, "specialcoursespecmappid"), "SpecialCourseSpecMapp", "specialcoursespecmappid", CommonMethods.OpType.新增);
                    CommonMethods.InsertTableChangeRecord_RB(dvSecSpec.ToTable(true, "specialcoursespecreportid"), "SpecialCourseSpecReport", "specialcoursespecreportid", CommonMethods.OpType.新增);


                    //保存第二层工艺信息
                    DataTable dtAttr1 = OracleHelper.QueryTable(string.Format(seleContainerAttrSql, containerID, "转工开卡二层工艺"), null);
                    if (dtAttr1.Rows.Count == 0)
                    {
                        OracleHelper.ExecuteSql(string.Format(insertContainerAttrSql, containerID, "转工开卡二层工艺", dtSecSpec.Rows[0]["workflowid"].ToString()));
                    }
                    else
                    {
                        OracleHelper.ExecuteSql(string.Format(updateContainerAttrSql, dtSecSpec.Rows[0]["workflowid"].ToString(), containerID, "转工开卡二层工艺"));
                    }
                    //保存第二层工序信息
                    DataTable dtAttr2 = OracleHelper.QueryTable(string.Format(seleContainerAttrSql, containerID, "转工开卡二层工序"), null);
                    if (dtAttr2.Rows.Count == 0)
                    {
                        OracleHelper.ExecuteSql(string.Format(insertContainerAttrSql, containerID, "转工开卡二层工序", dtSecSpec.Rows[0]["workflowstepid"].ToString()));
                    }
                    else
                    {
                        OracleHelper.ExecuteSql(string.Format(updateContainerAttrSql, dtSecSpec.Rows[0]["workflowstepid"].ToString(), containerID, "转工开卡二层工序"));
                    }
                    //推送该保存的信息
                    sql = string.Format("SELECT ca.containerattributeid FROM containerattribute ca WHERE ca.containerid = '{0}' AND (ca.attributename = '转工开卡二层工艺' OR ca.attributename = '转工开卡二层工序')", containerID);
                    CommonMethods.InsertTableChangeRecord_RB(OracleHelper.QueryTable(sql, null), "containerattribute", "containerattributeid", CommonMethods.OpType.新增);
                }
                else
                {
                    //如果不是三层工艺，直接推送批次本工艺对应的特殊报工即可
                    sql = string.Format(@"SELECT scs.specialcoursespecmappid,ssr.specialcoursespecreportid 
                                FROM SpecialCourseSpecMapp scs 
                                LEFT JOIN SpecialCourseSpecReport ssr ON ssr.specialcoursespecreportid = scs.specialcoursespecreportid
                                WHERE scs.workflowid = '{0}'", workflowID);
                    DataTable dtReport = OracleHelper.QueryTable(sql, null);//去第二层的工艺信息及特殊过程报告
                    DataView dvReport = dtReport.AsDataView();
                    CommonMethods.InsertTableChangeRecord_RB(dvReport.ToTable(true, "specialcoursespecmappid"), "SpecialCourseSpecMapp", "specialcoursespecmappid", CommonMethods.OpType.新增);
                    CommonMethods.InsertTableChangeRecord_RB(dvReport.ToTable(true, "specialcoursespecreportid"), "SpecialCourseSpecReport", "specialcoursespecreportid", CommonMethods.OpType.新增);
                }
            }
            else //推送采购订单批次的特殊过程
            {
                sql = string.Format(@"SELECT ww.workflowid,ww.workflowbaseid,ws.workflowstepid,sb.specbaseid,s.specid,scs.specialcoursespecmappid,ssr.specialcoursespecreportid
                        FROM containercurrentinfo c
                        LEFT JOIN Userattribute u2 ON u2.parentid = c.containerid AND u2.userattributename = '无订单开卡父工序ID'
                        LEFT JOIN Userattribute u3 ON u3.parentid = c.containerid AND u3.userattributename = '无订单开卡父工艺ID'
                        LEFT JOIN workflow ww ON ww.workflowid = u3.attributevalue
                        left join workflowstep ws on ws.workflowstepid= u2.attributevalue
                        left join specbase sb on sb.specbaseid=ws.specbaseid
                        left join spec s on s.specid=nvl(sb.revofrcdid,ws.specid)
                        LEFT JOIN SpecialCourseSpecMapp scs ON scs.workflowid = NVL(ww.workflowid,'{1}')
                        LEFT JOIN SpecialCourseSpecReport ssr ON ssr.specialcoursespecreportid = scs.specialcoursespecreportid AND ssr.indate > SYSDATE
                        WHERE c.containerid = '{0}'", containerID, workflowID);
                DataTable dtReport = OracleHelper.QueryTable(sql, null);
                DataView dvReport = dtReport.AsDataView();
                //推送第一层的工艺(可能为空)
                CommonMethods.InsertTableChangeRecord_RB(dvReport.ToTable(true, "workflowid"), "workflow", "workflowid", CommonMethods.OpType.新增);
                CommonMethods.InsertTableChangeRecord_RB(dvReport.ToTable(true, "workflowbaseid"), "workflowbase", "workflowbaseid", CommonMethods.OpType.新增);
                CommonMethods.InsertTableChangeRecord_RB(dvReport.ToTable(true, "workflowstepid"), "workflowstep", "workflowstepid", CommonMethods.OpType.新增);
                CommonMethods.InsertTableChangeRecord_RB(dvReport.ToTable(true, "specbaseid"), "specbase", "specbaseid", CommonMethods.OpType.新增);
                CommonMethods.InsertTableChangeRecord_RB(dvReport.ToTable(true, "specid"), "spec", "specid", CommonMethods.OpType.新增);
                //推送特殊过程时，如果有则更新推送，没有则新增推送
                CommonMethods.InsertTableChangeRecord_RB(dvReport.ToTable(true, "specialcoursespecmappid"), "SpecialCourseSpecMapp", "specialcoursespecmappid", CommonMethods.OpType.新增);
                CommonMethods.InsertTableChangeRecord_RB(dvReport.ToTable(true, "specialcoursespecreportid"), "SpecialCourseSpecReport", "specialcoursespecreportid", CommonMethods.OpType.新增);
            }
        }
        #endregion

        #region 其他数据推送（这些表数据记录在sync_tablechangerecord表中）

        /// <summary>
        /// 补充已经删除的批次，但未推送（这些表数据记录在sync_tablechangerecord表中）
        /// </summary>
        private void SendDeleContainer()
        {
            string sql = string.Format(@"SELECT c.containerid FROM container c 
                            WHERE c.containerid = c.containername AND c.originalfactoryid = '{0}' --AND c.originalstartdate >= SYSDATE - 2
                            AND NOT EXISTS(SELECT 1 FROM containercurrentinfo cc WHERE cc.containerid = c.containerid)
                            AND EXISTS(SELECT 1 FROM sync_tablechangerecord st WHERE st.tablename = UPPER('container') AND st.uniquecolvalue = c.containerid AND st.operatetype = 1)
                            AND NOT EXISTS(SELECT 1 FROM sync_tablechangerecord st WHERE st.tablename = UPPER('container') AND st.uniquecolvalue = c.containerid AND st.operatetype = 2)", rbFactoryID);
            DataTable dtCon = OracleHelper.QueryTable(sql, null);
            CommonMethods.InsertTableChangeRecord(dtCon, "container", "containerid", toAppId, CommonMethods.OpType.更新);
            CommonMethods.InsertTableChangeRecord(dtCon, "containercurrentinfo", "containerid", toAppId, CommonMethods.OpType.删除);
        }

        /// <summary>
        /// 推送部分表相关数据（这些表数据记录在sync_tablechangerecord表中）
        /// </summary>
        private void SendCommonTableData()
        {
            /*查询出委托计量表中已经有了qms返回委托单号的数据，并将数据推给工控网更新*/
            string sql = string.Format(@"SELECT e.entrustmeasurementid FROM EntrustMeasurement e 
                                WHERE e.factoryid = '{0}' AND e.qmsentrustname IS NOT NULL
                                AND EXISTS(SELECT 1 FROM RbPhysicoChemicalCheckInfo r WHERE r.productname = e.productname)
                                AND NOT EXISTS(SELECT 1 FROM sync_tablechangerecord st
                                    LEFT JOIN sync_sendtoappid so ON so.parentid = st.id 
                                    WHERE st.tablename = UPPER('EntrustMeasurement') AND st.uniquecolvalue = e.entrustmeasurementid AND so.toappid = '{1}')
                                ", rbFactoryID, toAppId);
            DataTable dtEntrustMeasurement = OracleHelper.QueryTable(sql, null);
            CommonMethods.InsertTableChangeRecord(dtEntrustMeasurement, "EntrustMeasurement", "entrustmeasurementid", toAppId, CommonMethods.OpType.更新);//只做更新操作
            /*推送人员信息*/
            sql = string.Format(@"SELECT Distinct e.employeeid 
                                    FROM employee e 
                                    WHERE e.orgfactoryid = '{0}'
                                    AND NOT EXISTS 
                                    (
                                            SELECT 1 FROM sync_tablechangerecord st 
                                            LEFT JOIN sync_sendtoappid so ON so.parentid = st.id
                                            WHERE st.tablename = UPPER('employee') AND st.uniquecolvalue = e.employeeid AND so.toappid = '{1}'
                                    )", rbFactoryID, toAppId);
            DataTable dtEmp = OracleHelper.QueryTable(sql, null);
            CommonMethods.InsertTableChangeRecord_RB(dtEmp, "employee", "employeeid", CommonMethods.OpType.新增);
            /*推送连续转工序的子卡关联关系表*/
            sql = string.Format(@"SELECT ca.containerattributeid FROM containerattribute ca
                                    LEFT JOIN containercurrentinfo c ON c.containername = to_char(ca.attributevalue)
                                    WHERE ca.attributename = '下序批次名' AND c.containerstatus = 1 AND c.originalfactoryid = '{0}'
                                    AND NOT EXISTS 
                                    (
                                            SELECT 1 FROM sync_tablechangerecord st 
                                            LEFT JOIN sync_sendtoappid so ON so.parentid = st.id
                                            WHERE st.tablename = UPPER('containerattribute') AND st.uniquecolvalue = ca.containerattributeid AND so.toappid = '{1}'
                                    )", rbFactoryID, toAppId);
            DataTable dtCon = OracleHelper.QueryTable(sql, null);
            CommonMethods.InsertTableChangeRecord(dtCon, "containerattribute", "containerattributeid", toAppId, CommonMethods.OpType.新增);
            /*查询问题快反的类型表并推送*/
            sql = string.Format(@"SELECT bo.id FROM BO_EU_MES_PROBLEM_TYPE bo 
                                  WHERE bo.factoryname LIKE '%热表%'
                                  AND NOT EXISTS(SELECT 1 FROM sync_tablechangerecord st
                                    LEFT JOIN sync_sendtoappid so ON so.parentid = st.id 
                                    WHERE st.tablename = UPPER('BO_EU_MES_PROBLEM_TYPE') AND st.uniquecolvalue = bo.id AND so.toappid = '{0}')", toAppId);
            DataTable dtProblemType = OracleHelper.QueryTable(sql, null);
            CommonMethods.InsertTableChangeRecord(dtProblemType, "BO_EU_MES_PROBLEM_TYPE", "id", toAppId, CommonMethods.OpType.新增);
            /*查询军检目录表并推送*/
            sql = string.Format(@"SELECT a.armycheckdirinfoid FROM armycheckdirinfo a
                                  WHERE a.isuse = 0 AND a.branchfactory LIKE '%热表%'
                                  AND NOT EXISTS(SELECT 1 FROM sync_tablechangerecord st
                                    LEFT JOIN sync_sendtoappid so ON so.parentid = st.id 
                                    WHERE st.tablename = UPPER('armycheckdirinfo') AND st.uniquecolvalue = a.armycheckdirinfoid AND so.toappid = '{0}')", toAppId);
            DataTable dtArmyCheck = OracleHelper.QueryTable(sql, null);
            CommonMethods.InsertTableChangeRecord(dtArmyCheck, "armycheckdirinfo", "armycheckdirinfoid", toAppId, CommonMethods.OpType.新增);
            /*查询热表专项计划表并推送*/
            sql = string.Format(@"SELECT r.id FROM rbspeciallyitemplan r WHERE r.itemresource = '改善计划' OR r.itemresource = '培训计划'
                                  AND NOT EXISTS(SELECT 1 FROM sync_tablechangerecord st
                                  LEFT JOIN sync_sendtoappid so ON so.parentid = st.id 
                                  WHERE st.tablename = UPPER('rbspeciallyitemplan') AND st.uniquecolvalue = r.id AND so.toappid = '{0}')", toAppId);
            DataTable dtItemPlan = OracleHelper.QueryTable(sql, null);
            CommonMethods.InsertTableChangeRecord(dtItemPlan, "rbspeciallyitemplan", "id", toAppId, CommonMethods.OpType.新增);
            /*推送委托计量附件*/
            sql = string.Format(@"SELECT e.entrustmeasurementid,e.reporturl FROM EntrustMeasurement e 
                                WHERE e.factoryid = '{0}' AND e.qmsentrustname IS NOT NULL AND e.reporturl IS NOT NULL
                                AND EXISTS(SELECT 1 FROM RbPhysicoChemicalCheckInfo r WHERE r.productname = e.productname)
                                AND NOT EXISTS(SELECT 1 FROM objectdocinfo ob WHERE ob.parentid = e.entrustmeasurementid)", rbFactoryID);
            DataTable dtEntrustObj = OracleHelper.QueryTable(sql, null);//查询未推送的附件
            ArrayList sqlList = new ArrayList();
            DataTable dtObj = new DataTable();
            dtObj.Columns.Add("OBJECTDOCINFOID");
            foreach (DataRow item in dtEntrustObj.Rows)
            {
                string fileName = string.Format("热表理化{0}.pdf", DateTime.Now.ToString("yyyyMMddHHmmssffff"));//文件名
                string objID = Guid.NewGuid().ToString();//附件表ID
                sqlList.Add(string.Format(@"INSERT INTO OBJECTDOCINFO(OBJECTDOCINFOID,FILENAME,FILEPATH,PARENTID) VALUES('{0}','{1}','{2}','{3}')",
                    objID, fileName, item["ReportUrl"].ToString(), item["entrustmeasurementid"].ToString()));
                dtObj.Rows.Add(objID);
                System.Threading.Thread.Sleep(1000);//确保文件名不重复
            }
            OracleHelper.ExecuteSqlTran(sqlList);
            CommonMethods.InsertTableChangeRecord(dtObj, "OBJECTDOCINFO", "OBJECTDOCINFOID", toAppId, CommonMethods.OpType.新增);
        }

        /// <summary>
        /// 推送热表工艺变更暂停或者释放的数据
        /// 说明：批次暂停时需要将opercontainerrecord表暂停原因推送过去，批次释放后有需要将工控网的opercontainerrecord表删除掉，
        /// 确保工控网opercontainerrecord表在批次暂停时只有一条数据，释放后就没有数据了。
        /// </summary>
        private void SendWfChangeHoldOrReleaseData()
        {
            /*1、推送热表工艺变更暂停的批次，并将暂停原因表数据推送过去*/
            string sql = string.Format(@"SELECT distinct c.containerid,oc.operatorid
                            FROM opercontainerrecord oc
                            LEFT JOIN containercurrentinfo c ON c.containerid = oc.containerid
                            INNER JOIN (
                            SELECT MAX(oc2.createdate) createdate,oc2.containerid FROM opercontainerrecord oc2 WHERE oc2.opertype = '2' GROUP BY oc2.containerid)
                            moc ON moc.containerid = oc.containerid AND moc.createdate = oc.createdate --关联批次的最新暂停必须是工艺变更暂停
                            WHERE oc.opertype = '2' AND c.containerstatus = 1 AND c.isinstore != 1 AND c.ishold = 1
                            AND c.originalfactoryid = '{0}' AND oc.operreason = '工艺变更暂停'
                            AND NOT EXISTS(SELECT 1 FROM opercontainerrecord oc1 WHERE oc1.opertype = '3' AND oc1.operreason = '工艺变更释放' AND oc1.containerid = oc.containerid AND oc1.createdate > oc.createdate)
                            AND NOT EXISTS(SELECT 1 FROM sync_tablechangerecord st
                                    LEFT JOIN sync_sendtoappid so ON so.parentid = st.id 
                                    WHERE st.tablename = UPPER('opercontainerrecord') AND st.uniquecolvalue = oc.operatorid AND so.toappid = '{1}')", rbFactoryID, toAppId);
            DataTable dtHold = OracleHelper.QueryTable(sql, null);
            CommonMethods.InsertTableChangeRecord(dtHold.AsDataView().ToTable(true, "operatorid"), "opercontainerrecord", "operatorid", toAppId, CommonMethods.OpType.新增);
            CommonMethods.InsertTableChangeRecord(dtHold.AsDataView().ToTable(true, "containerid"), "containercurrentinfo", "containerid", toAppId, CommonMethods.OpType.更新);
            /*2、推送热表工艺变更释放的批次*/
            sql = string.Format(@"SELECT distinct c.containerid,ca.recordid
                            FROM containerreleaseauditrecord ca
                            LEFT JOIN containercurrentinfo c ON c.containerid = ca.containerid
                            WHERE ca.status = 3 AND c.containerstatus = 1 AND c.isinstore != 1 AND c.ishold = 0
                            AND c.originalfactoryid = '{0}'
                            AND NOT EXISTS(SELECT 1 FROM sync_tablechangerecord st
                                    LEFT JOIN sync_sendtoappid so ON so.parentid = st.id 
                                    WHERE st.tablename = UPPER('containerreleaseauditrecord') AND st.uniquecolvalue = ca.recordid AND so.toappid = '{1}')", rbFactoryID, toAppId);
            DataTable dtRelCon = OracleHelper.QueryTable(sql, null);
            CommonMethods.InsertTableChangeRecord(dtRelCon.AsDataView().ToTable(true, "recordid"), "containerreleaseauditrecord", "recordid", toAppId, CommonMethods.OpType.新增);//
            CommonMethods.InsertTableChangeRecord(dtRelCon.AsDataView().ToTable(true, "containerid"), "containercurrentinfo", "containerid", toAppId, CommonMethods.OpType.更新);//更新批次信息
            /*3、将之前推送暂停的表数据标识为删除推送过去（批次已经释放，确保工控网没有opercontainerrecord这个表的数据，达到工控网唯一性）*/
            sql = string.Format(@"SELECT oc.operatorid FROM opercontainerrecord oc 
                                    WHERE oc.containerid = '{0}'
                                    AND EXISTS(SELECT 1 FROM sync_tablechangerecord st
                                        LEFT JOIN sync_sendtoappid so ON so.parentid = st.id 
                                        WHERE st.tablename = UPPER('opercontainerrecord') AND st.uniquecolvalue = oc.operatorid AND so.toappid = '{1}')");
            foreach (DataRow item in dtRelCon.AsDataView().ToTable(true, "containerid").Rows)
            {
                DataTable dtOper = OracleHelper.QueryTable(string.Format(sql, item["containerid"].ToString(), toAppId), null);
                CommonMethods.InsertTableChangeRecord(dtOper, "opercontainerrecord", "operatorid", toAppId, CommonMethods.OpType.删除);
            }
        }
        #endregion
    }
}
